<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class HelpRequest extends Model
{

    protected $fillable = [
        'user_id',
        'title',
        'description',
        'is_urgent',
        'is_anonymous',
        'status',
        'ai_suggestion',
        'best_answer_id',
        'is_hidden'
    ];

    protected $casts = [
        'is_urgent' => 'boolean',
        'is_anonymous' => 'boolean',
        'best_answer_id' => 'integer',
        'is_hidden' => 'boolean',
        'created_at' => 'datetime',
        'updated_at' => 'datetime'
    ];

    /**
     * 关联用户
     */
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    /**
     * 关联回答
     */
    public function answers()
    {
        return $this->hasMany(HelpAnswer::class);
    }

    /**
     * 获取最佳回答
     */
    public function bestAnswer()
    {
        return $this->hasOne(HelpAnswer::class)->where('is_best_answer', true);
    }

    /**
     * 检查是否已解决
     */
    public function isResolved()
    {
        return $this->status === 'resolved';
    }

    /**
     * 检查用户是否已收藏
     */
    public function isBookmarkedBy($userId)
    {
        // 这里可以添加收藏逻辑
        return false;
    }
}